package com.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileInputStream;
import java.util.*;

@Slf4j
public class ATLASUtils {





        public static List< Map<String, Object>> importExcel(String filePath) throws Exception {
            File file = new File( filePath);
            Workbook wb = null;
            String fileName = file.getName();// 读取上传文件(excel)的名字，含后缀后
            // 根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象
            Iterator<Sheet> sheets = null;
            List< Map<String, Object>> returnlist = new ArrayList<>();
            try {

                wb = new XSSFWorkbook(new FileInputStream(file));
                sheets = wb.iterator();

                if (sheets == null) {
                    throw new Exception("excel中不含有sheet工作表");
                }
                // 遍历excel里每个sheet的数据。
                while (sheets.hasNext()) {
                    Sheet sheet = sheets.next();
                    Map<String, Object> cellValue = getCellValue(sheet);
                    returnlist.add(cellValue);
                }
            } catch (Exception ex) {
                throw ex;
            } finally {
                if (wb != null) {
                    wb.close();
                }
            }
            return returnlist;
        }


        // 获取每一个Sheet工作表中的数。
        private static  Map<String,Object>  getCellValue(Sheet sheet) {
            final String tableName = sheet.getSheetName();
            if("视图名称".equals(tableName)){
                return  null;
            }
            Map<String,Object> tableInfo=new HashMap<>(2);

            tableInfo.put("tableName",tableName);
            List<FRDEntity> returnMap=new ArrayList<>();
            // sheet.getPhysicalNumberOfRows():获取的是物理行数，也就是不包括那些空行（隔行）的情况
//            final int firstRowNum = sheet.getFirstRowNum();
            final int physicalNumberOfRows = sheet.getPhysicalNumberOfRows();
            int firstRowNum=1;
            for (int i =firstRowNum ; i < physicalNumberOfRows; i++) {
                Map map = new HashMap<>();
                // 获得第i行对象
                Row row = sheet.getRow(i);
                if (row == null) {
                    continue;
                } else {
                    // getLastCellNum：获取列数(比最后一列列标大1)
                    // 通常导入的excel是符合需求规范的，这里因为我们准备的excel列数是7列，如果小于7列说明传入的excel文件有问题。
                    if (row.getLastCellNum() < 7) {
                        continue;
                    }
                    FRDEntity frdEntity=new FRDEntity();
                    final String fieldName = String.valueOf(getValue(row, 0));
                    if("Field Name".equals(fieldName)||"null".equals(fieldName)||"Field_Name".equals(fieldName)||"Field Name".equals(fieldName)){
                        continue;
                    }
                    frdEntity.setFieldName(fieldName);
                    frdEntity.setDes(String.valueOf(getValue(row, 1)));

                    frdEntity.setFieldType(String.valueOf(getValue(row, 4)));

                      Object value = getValue(row, 6);
                    if(value==null||value.equals("null")){
                        value=0;
                    }
                    if(value instanceof  Integer){
                        value = ((Integer) value).intValue()>1024?1023:value;

                    }
                    frdEntity.setFieldLength(String.valueOf(value));
                    final Object isRequired = getValue(row, 4);
                    frdEntity.setIsRequired(isRequired ==null&& String.valueOf(isRequired).trim().equals("Y"));
                    returnMap.add(frdEntity);
                }
            }
            tableInfo.put("colum",returnMap);
            return tableInfo;
        }

    private static Object getValue(Row row, int k) {
        Cell cell2 = row.getCell(k);
        Object obj = null;
        if (cell2 != null) { // getCell 获取单元格数据
            switch (cell2.getCellType()) {
                case BOOLEAN:
                    obj = cell2.getBooleanCellValue();
                    break;
                case NUMERIC:
                    Double numericCellValue = cell2.getNumericCellValue();
                    obj = numericCellValue.intValue();
                    break;
                case STRING:
                    obj = cell2.getStringCellValue();
                    break;
                case BLANK:
                    break;
                case ERROR:
                    obj = cell2.getErrorCellValue();
                    break;
            }
        } else {
            return null ;
        }

        return obj;
    }
}
